from urllib import parse
from util.get_time_stamp import get_time_stamp
import requests
import hashlib
import execjs


def get_quote(s):
    return parse.quote(s)

def get_data(catalog='角钢', city='福州', spec = 'Q235B 50*50*5'):
    url = 'https://index.mysteel.com/api/pricetrend/getChartMultiCity.htm'

    # 使用python实现加密参数
    ts = get_time_stamp()
    path = '/api/pricetrend/getChartMultiCity.htm'
    appSec = '97e3d765c8574f35'
    path = "path" + path + "timestamp" + f'{ts}' + "version1.0.0" + appSec
    sign = hashlib.md5(path.encode('utf-8')).hexdigest().upper()

    res = requests.get(url, params={
        "catalog": f"{get_quote(catalog)}_:_{get_quote(catalog)}",
        "city": f"{get_quote(city)}",
        "spec": f"{get_quote(spec)}_:_{spec.replace(' ', '_')}",
        "startTime": "2024-11-1",
        "endTime": "2025-11-25",
        "callback": "json",
        "v": ts,
        "Api-Token": sign,
        "Timestamp": ts
    })

    res = res.json()

    with open(f'./parse/4.钢铁网.js', encoding='utf8') as f:
        js_str = f.read()
        js_code = execjs.compile(js_str, cwd='./parse')

    data = js_code.call("decrypt", res)
    return data


print(get_data())